/**
 * 整个项目api的管理
 *
 */
import config from "@/config";
import request from "./request";
import mockHome from "@/api/mockData/home";
import mockUser from "@/api/mockData/user";

export default {
  // home组件 左侧表格数据获取
  getTableData() {
    if (config.mock) {
      // 返回本地 mock 数据
      return Promise.resolve(mockHome.getTableData());
    }
    // 真实接口请求
    return request({
      url: "/home/getTable",
      method: "get",
    });
  },
  getCountData() {
    if (config.mock) {
      // 返回本地 mock 数据
      return Promise.resolve(mockHome.getCountData());
    }
    // 真实接口请求
    return request({
      url: "/home/getCount",
      method: "get",
    });
  },
  getChartData() {
    if (config.mock) {
      // 返回本地 mock 数据
      return Promise.resolve(mockHome.getChartData());
    }
    // 真实接口请求
    return request({
      url: "/home/getChart",
      method: "get",
    });
  },
  getUserData(params = {}) {
    if (config.mock) {
      // 拼接查询参数到 url
      const query = new URLSearchParams(params).toString();
      const url = "/home/getUser" + (query ? `?${query}` : "");
      return Promise.resolve(mockUser.getUserList({ url }));
    }
    // 真实接口请求
    return request({
      url: "/home/getUser",
      method: "get",
      params,
    });
  },
  deleteUser(params: any) {
    if (config.mock) {
      return Promise.resolve(mockUser.deleteUser(params));
    }
    // 真实接口请求
    return request({
      url: "/home/deleteUser",
      method: "post",
      data: params,
    });
  },
  addUser(params: any) {
    if (config.mock) {
      return Promise.resolve(mockUser.createUser(params));
    }
    // 真实接口请求
    return request({
      url: "/home/createUser",
      method: "post",
      data: params,
    });
  },
  updateUser(params: any) {
    if (config.mock) {
      return Promise.resolve(mockUser.updateUser(params));
    }
    // 真实接口请求
    return request;
  },
  getMenu(params : any) {
    return request({
      url: "/permission/getMenu",
      method: "post",
      data: params,
    });
  },
};
